<?php

namespace app\admin\model;

use app\admin\model\bin\Native;
use app\admin\model\bin\Type;
use think\Model;


class Bin extends Model
{

    

    

    // 表名
    protected $name = 'bin';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'currency_type_text',
        'current_status_text',
        'last_status_text',
        'bin_origin_text',
        'bin_type_text',
        'bin_native_text',
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }

    public function getBinOriginList()
    {
        return ['1' => __('Bin_origin 1'), '2' => __('Bin_origin 2')];
    }

    public function getCurrencyTypeList()
    {
        return ['1' => __('Currency_type 1'), '2' => __('Currency_type 2'), '3' => __('Currency_type 3'), '4' => __('Currency_type 4')];
    }


    public function getCurrentStatusList()
    {
        return ['1' => __('Current_status 1'), '2' => __('Current_status 2'), '3' => __('Current_status 3'), '4' => __('Current_status 4'), '5' => __('Current_status 5')];
    }

    public function getLastStatusList()
    {
        return ['1' => __('Last_status 1'), '2' => __('Last_status 2'), '3' => __('Last_status 3'), '4' => __('Last_status 4'),'5' => __('Last_status 5'),'6' => __('Last_status 6'),'7' => __('Last_status 7'),'8' => __('Last_status 8')];
    }

    public function getBinTypeList()
    {
        $typeList=Type::order(['weigh'=>'desc','createtime','id'])->select();

        return $typeList;
    }

    public function getBinNativeList()
    {
        $nativeList=Native::order(['weigh'=>'desc','createtime','id'])->select();

        return $nativeList;
    }

    public function getBinOriginTextAttr($value, $data){
        $value = $value ? $value : (isset($data['bin_origin']) ? $data['bin_origin'] : '');
        $list = $this->getBinOriginList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getCurrencyTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['currency_type']) ? $data['currency_type'] : '');
        $list = $this->getCurrencyTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }



    public function getCurrentStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['current_status']) ? $data['current_status'] : '');
        $list = $this->getCurrentStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getLastStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['last_status']) ? $data['last_status'] : '');
        $list = $this->getLastStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getBinTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['bin_type']) ? $data['bin_type'] : '');
        $list = $this->getBinTypeList();
        $name='';
        if(isset($list[$value])){
            $data = json_decode($list[$value], true);
            $name=$data['name'];
        }
        return $name;
    }


    public function getBinNativeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['bin_native']) ? $data['bin_native'] : '');
        $list = $this->getBinNativeList();
        $name='';
        if(isset($list[$value])){
            $data = json_decode($list[$value], true);
            $name=$data['name'];
        }
        return $name;
    }

    public function type()
    {
        return $this->belongsTo('app\admin\model\bin\Type', 'bin_type', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function native()
    {
        return $this->belongsTo('app\admin\model\bin\Native', 'bin_native', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function bincompany()
    {
        return $this->belongsTo('app\admin\model\Organization', 'bin_company_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function ourcompany()
    {
        return $this->belongsTo('app\admin\model\Organization', 'our_company_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function yard()
    {
        return $this->belongsTo('app\admin\model\Organization', 'yard_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
 public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'performance_person', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function order()
    {
        return $this->belongsTo('app\admin\model\order\Purchase', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function sellorder()
    {
        return $this->hasOne('app\admin\model\bin\Sell', 'bin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


}
